$(function () {
    $("#jqGrid").jqGrid({
        url: '../material/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '1---theme 2---videofx 3---captionstyle 4---animatedsticker 5---videotransition 6---font 7---music 8---capture scene 9---particle 10---face sticker 11—faceu zip 12—faceu bundle 13—superzoom 14—arscene 15—compound caption', name: 'materialType', index: 'material_type', width: 80 }, 			
			{ label: '', name: 'category', index: 'category', width: 80 }, 			
			{ label: '', name: 'displayName', index: 'display_name', width: 80 }, 			
			{ label: '', name: 'displayNameZhCn', index: 'display_name_zh_CN', width: 80 }, 			
			{ label: '', name: 'commodityNumber', index: 'commodity_number', width: 80 }, 			
			{ label: '', name: 'description', index: 'description', width: 80 }, 			
			{ label: '', name: 'tags', index: 'tags', width: 80 }, 			
			{ label: '', name: 'tagsZhCn', index: 'tags_zh_CN', width: 80 }, 			
			{ label: '', name: 'version', index: 'version', width: 80 }, 			
			{ label: '', name: 'minAppVersion', index: 'min_app_version', width: 80 }, 			
			{ label: '', name: 'packageFileName', index: 'package_file_name', width: 80 }, 			
			{ label: '', name: 'packageUrl', index: 'package_url', width: 80 }, 			
			{ label: '', name: 'packageSize', index: 'package_size', width: 80 }, 			
			{ label: '', name: 'cover', index: 'cover', width: 80 }, 			
			{ label: '', name: 'coverUrl', index: 'cover_url', width: 80 }, 			
			{ label: '', name: 'coverUrl2', index: 'cover_url2', width: 80 }, 			
			{ label: '特定素材(例如影集、mimo等)相关视频URL', name: 'videoUrl', index: 'video_url', width: 80 }, 			
			{ label: '特定素材(例如影集、mimo等)相关元数据信息URL', name: 'infoUrl', index: 'info_url', width: 80 }, 			
			{ label: '组成此素材(例如mimo等)的子素材的UUID拼接字符串(逗号分隔)', name: 'childrenIds', index: 'children_ids', width: 80 }, 			
			{ label: '', name: 'idx', index: 'idx', width: 80 }, 			
			{ label: '', name: 'lastUpdateTime', index: 'last_update_time', width: 80 }, 			
			{ label: 'Bitwise OR of supported aspect ratio: 1 --- 16v9 2 --- 1v1 4 — 9v16 8 — 4v3 16 — 3v4 32— 18v9 64— 9v18', name: 'supportedAspectRatio', index: 'supported_aspect_ratio', width: 80 }, 			
			{ label: '', name: 'errorFlag', index: 'error_flag', width: 80 }, 			
			{ label: '', name: 'public', index: 'public', width: 80 }, 			
			{ label: '0 — 非通用 1 — 通用', name: 'ratioFlag', index: 'ratio_flag', width: 80 }, 			
			{ label: '0 — 老的贴纸模板，为快VR需要 1 -- 新的贴纸模板，bilibili需要', name: 'customStickerTemplateFlag', index: 'custom_sticker_template_flag', width: 80 }, 			
			{ label: '属于某些用户的私有素材，逗号分隔', name: 'userId', index: 'user_id', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		material: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.material = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.material.id == null ? "../material/save" : "../material/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.material),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../material/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../material/info/"+id, function(r){
                vm.material = r.material;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});